/*
2022-3-6
https://leetcode-cn.com/problems/create-binary-tree-from-descriptions/
*/ 
class Solution {
public:
    TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
        unordered_map<int,TreeNode*> mp;
        unordered_map<int,int> d;
        for(auto a:descriptions)
        {
            int p=a[0],c=a[1],l=d[2];
            if(!mp.count(p)) mp[p]=new TreeNode(p);
            if(!mp.count(c)) mp[p]=new TreeNode(c);
            if(l) mp[p]->left=mp[c];
            else mp[p]->right=mp[c];
            d[c]++;
        }

        for(auto &[val,node]:mp)
        {
            if(!d.count(val))
                return node;
        }
        return nullptr;
    }
};

